#!/usr/bin/env python
'''
program to draw cumulant for quantum critical point
'''
from __future__ import division

import matplotlib.pyplot
import os
import sys

sys.path += [ '..' ]

import common
import math
import scipy

path = os.path.join('..', '..', 'results')


modelname = common.choice(path)
J = common.choice(os.path.join(path, modelname))

T = 0.5


N20 = []
N16 = []
N14 = []

for tB in os.listdir(os.path.join(path, modelname, J)):
    B = tB.replace('B_', '')
    
    try:
        open( os.path.join( os.path.join(path, modelname, J, tB, 'N16', 'dT0.05', 'M2', 'T0.5')  ) ).read()
    except:
        pass
    else:
        path16 = os.path.join( os.path.join(path, modelname, J, tB, 'N16', 'dT0.05')  )
        M2 = eval( open(os.path.join( path16, 'M2', 'T0.5')).read())
        M4 = eval( open(os.path.join( path16, 'M4', 'T0.5')).read())    
        N16 += [ (B, 1 - M4 / (3 * M2**2) ) ]
    try:
        open( os.path.join( os.path.join(path, modelname, J, tB, 'N14', 'dT0.05', 'M2', 'T0.5')  ) ).read()
    except:
        pass
    else:
        path14 = os.path.join( os.path.join(path, modelname, J, tB, 'N14', 'dT0.05')  )
        M2 = eval( open(os.path.join( path14, 'M2', 'T0.5')).read())
        M4 = eval( open(os.path.join( path14, 'M4', 'T0.5')).read())
        
        N14 += [ (B, 1 - M4 / (3 * M2**2) ) ]
    
    try:
        open(os.path.join( os.path.join(path, modelname, J, tB, 'N20', 'dT0.05', 'M2', 'T0.5')  )).read()
    except:
        pass
    else:
        path20 = os.path.join( os.path.join(path, modelname, J, tB, 'N20', 'dT0.05')  )
        M2 = eval( open(os.path.join( path20, 'M2', 'T0.5')).read())
        M4 = eval( open(os.path.join( path20, 'M4', 'T0.5')).read())
        
        N20 += [ (B, 1 - M4 / (3 * M2**2) ) ]
    
N14.sort()

B14 = [ tt[0] for tt in N14]
Cum14 = [ tt[1] for tt in N14]

matplotlib.pyplot.plot(B14, Cum14, label = '$N = 14$')

N16.sort()
B16 = [ tt[0] for tt in N16]
Cum16 = [ tt[1] for tt in N16]

matplotlib.pyplot.plot(B16, Cum16, label = '$N = 16$')

N20.sort()

B20 = [ tt[0] for tt in N20]
Cum20 = [ tt[1] for tt in N20]

matplotlib.pyplot.plot(B20, Cum20, label = '$N = 20$')
matplotlib.pyplot.xlabel('$B$')
matplotlib.pyplot.ylabel('$Cumulant$')

matplotlib.pyplot.legend()

matplotlib.pyplot.show()
